package mbalib.ui.managers
{
	import flash.display.*;
	import flash.events.*;
	import flash.ui.*;
	import flash.utils.*;
	
	public class CursorManager extends Object
	{
		private var tigger2Cursor:Dictionary;
		private var _root:DisplayObjectContainer = null;
		private var container:DisplayObjectContainer = null;
		private var currentCursor:DisplayObject = null;
		
		public function CursorManager()
		{
			this.tigger2Cursor = new Dictionary(true);
			this.container = new Sprite();
			this.container.mouseEnabled = false;
			this.container.tabEnabled = false;
			this.container.mouseChildren = false;
			return;
		}// end function
		
		public function get root() : DisplayObjectContainer
		{
			return this._root;
		}// end function
		
		public function set root(param1:DisplayObjectContainer) : void
		{
			if (this._root == null)
			{
				this._root = param1;
				this._root.addChild(this.container);
			}
			else
			{
				trace("Error:root只允许设置一次，之后不允许修改。");
			}
			return;
		}// end function
		
		public function showCursor(param1, param2:Boolean = true) : void
		{
			param1 = InstanceManager.createSingletonInstance(param1);
			if (param2)
			{
				Mouse.hide();
			}
			else
			{
				Mouse.show();
			}
			if (param1 == this.currentCursor)
			{
				return;
			}
			if (this.container != null)
			{
				if (this.currentCursor != param1)
				{
					if (this.currentCursor != null)
					{
						this.container.removeChild(this.currentCursor);
					}
					this.currentCursor = param1;
					this.container.addChild(this.currentCursor);
				}
				DepthManager.bringToTop(this.container);
				this.root.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.handleMove, false);
				this.handleMove(null);
			}
			return;
		}// end function
		
		private function handleMove(event:MouseEvent) : void
		{
			this.container.x = this.container.parent.mouseX;
			this.container.y = this.container.parent.mouseY;
			DepthManager.bringToTop(this.container);
			if (event != null)
			{
				event.updateAfterEvent();
			}
			return;
		}// end function
		
		public function hideCursor(param1) : void
		{
			param1 = InstanceManager.createSingletonInstance(param1);
			if (param1 != this.currentCursor)
			{
				return;
			}
			this.hideCurrentCursor();
			return;
		}// end function
		
		public function hideCurrentCursor() : void
		{
			if (this.container != null)
			{
				if (this.currentCursor != null)
				{
					this.container.removeChild(this.currentCursor);
				}
			}
			this.currentCursor = null;
			Mouse.show();
			if (this.root != null)
			{
				this.root.stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.handleMove);
			}
			return;
		}// end function
		
		public function register(param1:InteractiveObject, param2) : void
		{
			if (param2 != null)
			{
				this.tigger2Cursor[param1] = InstanceManager.createSingletonInstance(param2);
				param1.addEventListener(MouseEvent.ROLL_OVER, this.handleTriggerOver, false);
				param1.addEventListener(MouseEvent.ROLL_OUT, this.handleTriggerOut, false);
				param1.addEventListener(MouseEvent.MOUSE_UP, this.handleTriggerUp, false);
			}
			else
			{
				this.handleTriggerOut(null);
				param1.removeEventListener(MouseEvent.ROLL_OVER, this.handleTriggerOver, false);
				param1.removeEventListener(MouseEvent.ROLL_OUT, this.handleTriggerOut, false);
				param1.removeEventListener(MouseEvent.MOUSE_UP, this.handleTriggerUp, false);
				delete this.tigger2Cursor[param1];
			}
			return;
		}// end function
		
		public function unregister(param1:InteractiveObject) : void
		{
			this.register(param1, null);
			return;
		}// end function
		
		private function handleTriggerOver(event:MouseEvent) : void
		{
			var _loc_2:* = event.currentTarget as InteractiveObject;
			var _loc_3:* = this.tigger2Cursor[_loc_2] as DisplayObject;
			if (_loc_3 && !event.buttonDown)
			{
				this.showCursor(_loc_3);
			}
			return;
		}// end function
		
		private function handleTriggerOut(event:MouseEvent) : void
		{
			var _loc_2:* = event.currentTarget as InteractiveObject;
			var _loc_3:* = this.tigger2Cursor[_loc_2] as DisplayObject;
			if (_loc_3)
			{
				this.hideCursor(_loc_3);
			}
			return;
		}// end function
		
		private function handleTriggerUp(event:MouseEvent) : void
		{
			var _loc_2:* = event.currentTarget as InteractiveObject;
			var _loc_3:* = this.tigger2Cursor[_loc_2] as DisplayObject;
			if (_loc_3 && _loc_2.hitTestPoint(event.stageX, event.stageY, true))
			{
				this.showCursor(_loc_3);
			}
			return;
		}// end function
		
		public static function getInstance() : CursorManager
		{
			return InstanceManager.createSingletonInstance(CursorManager);
		}// end function
		
	}
}
